<template>
  <div class="homeSearch">
    <!-- 位置 -->
    <div class="location">
        <div class="city" @click="cityClick">{{currentCity.cityName}}</div>
        <div class="position" @click="positionClick">
            <span class="text">我的位置</span>
            <img src="@/assets/img/home/icon_location.png" alt="">
        </div>
    </div>
    <!-- 日期 -->
    <div class="section date-range bottom-gray-line" @click="showCalendar = true">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ startDate }}</span>
        </div>
        <div class="stay">共{{stayCount}}晚</div>
      </div>
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time">{{ endDate }}</span>
        </div>
      </div>
    </div>
    <!-- 选择日期 -->
    <van-calendar 
      v-model:show="showCalendar" 
      color="#ff9854" 
      type="range" 
      @confirm="onConfirm"
      :formatter="formatter"
     />
     <!-- 价格/人数选择 -->
    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <!-- 关键字 -->
    <div class="section keyword bottom-gray-line">关键字/位置/民宿名</div>
    <!-- 热门建议 -->
    <div class="section hot-suggests">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div 
          class="item"
          :style="{ color: item.tagText.color, background: item.tagText.background.color }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>
    <!-- 搜索按钮 -->
    <div style="margin: 10px 20px;">
      <van-button  round color="var(--theme-linear-gradient)" block @click="toSearch">开始搜索</van-button>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router"
import useCityStore from '@/stores/modules/city';
import useMainStore from '@/stores/modules/main';
import { storeToRefs } from "pinia";
import { computed, ref } from "vue";
import { formatMonthDay, getDiffDays } from "@/utils/format_date"
import useHomeStore from "@/stores/modules/home"

const router = useRouter()

// 点击进入城市
const cityClick = () => {
    router.push("/city")
}
// 获取当前位置, 经纬度
const positionClick = () => {
    navigator.geolocation.getCurrentPosition(res => {
        console.log("获取位置成功：", res)
    }, err => {
        console.log("获取位置失败：", err)
    },{
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
    })
}
// 获取当前城市信息
const cityStore = useCityStore()
const { currentCity } = storeToRefs(cityStore)

// 日期
const mainStore = useMainStore()
const { startDateStr, endDateStr } = storeToRefs(mainStore)

const startDate = computed(() => formatMonthDay(startDateStr.value)) 
const endDate = computed(() => formatMonthDay(endDateStr.value)) 
const stayCount = ref(getDiffDays(startDateStr.value, endDateStr.value))  //计算一共多少天
const showCalendar = ref(false) //默认不显示选择日期
const formatter = (day) => {  // 改变默认开始结束
  if (day.type === 'start') {
    day.bottomInfo = '入住';
  } else if (day.type === 'end') {
    day.bottomInfo = '离店';
  }
   return day;
}
const onConfirm = (data) => { //日期点击确定
  const start = data[0]
  const end = data[1]
  mainStore.startDateStr = start
  mainStore.endDateStr = end
  stayCount.value = getDiffDays(start, end)
  showCalendar.value = false
}

// 热门建议
const homeStore = useHomeStore()
const { hotSuggests } = storeToRefs(homeStore)

// 点击开始搜索，跳转搜索页面
const toSearch = () => {
  router.push({
    path: "/search",
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value.cityName
    }
  })
}

</script>

<style lang="less" scoped>
// 位置
.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    flex: 1;
    color: #333;
  }

  .position {
    width: 78px;
    display: flex;
    align-items: center;

    .text {
      position: relative;
      top: 2px;
      font-size: 14px;
      color: #666;
    }

    img {
      margin-left: 3px;
      width: 18px;
      height: 18px;
    }
  }
}

//日期
.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;
  height: 44px;

  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
  }

  .end {
    min-width: 30%;
    padding-left: 20px;
  }

  .date {
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
      color: #999;
    }

    .time {
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
} 
.date-range {
  height: 44px;
  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}

// 价格/人数选择
.price-counter {
  .start {
    border-right: 1px solid  var(--line-color);
  }
}

// 热门推荐
.hot-suggests {
  margin: 10px 0;
  height: auto;

  .item {
    padding: 4px 8px;
    margin: 4px;
    border-radius: 14px;
    font-size: 12px;
    line-height: 1;
  }
}
</style>